package org.restoran.model;

import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.restoran.service.parser.jackson.IdentityJsonSerialyzer;
import org.restoran.service.parser.jackson.OrderRefBeanDeserializer;

import javax.persistence.*;

/**
 * @author Snisar Bogdan.
 */
@Entity
@javax.persistence.Table(name = "DESK")
public class Table extends NameEntity {

    @Column(name = "DESK_X")
    private Integer x;

    @Column(name = "DESK_Y")
    private Integer y;

    @JsonDeserialize(using = OrderRefBeanDeserializer.class)
    @JsonSerialize(using = IdentityJsonSerialyzer.class)
    @JsonProperty(value = "orderId")
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "ORDER_ID", nullable = true)
    private Order order;

    @Enumerated(EnumType.STRING)
    @Column(name = "STATUS")
    private TableStatus status;

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public TableStatus getStatus() {
        return status;
    }

    public void setStatus(TableStatus status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Table{" +
                "x=" + x +
                ", y=" + y +
                ", order=" + order +
                ", status=" + status +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Table)) return false;

        Table table = (Table) o;

        if (status != table.status) return false;
        if (x != null ? !x.equals(table.x) : table.x != null) return false;
        if (y != null ? !y.equals(table.y) : table.y != null) return false;
        if(getId() != null ? !getId().equals(table.getId()) : table.getId() != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = x != null ? x.hashCode() : 0;
        result = 31 * result + (y != null ? y.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }
}
